#include "Reactor.hpp"
#include "Listener.hpp"
#include "HandlerConnection.hpp"
#include "NetCal.hpp"
#include <memory>

int main(int argc, char* argv[]) {
    if (argc != 2) {
        LOG(ERROR, "please input: ./server port\n");
        return 1;
    }
    uint16_t port = std::stoi(argv[1]);
    NetCal cal;
    calculate_t calculator = std::bind(&NetCal::Calculator, &cal, std::placeholders::_1);
    IOService io_forward;
    HandlerConnection handler_connection(std::bind(&IOService::IOForword, &io_forward, calculator, std::placeholders::_1));

    // 将数据接收不过
    // 然后转发出去
    // 让客户端接收之后处理即可

    std::unique_ptr<Reactor> R = std::make_unique<Reactor>(port);

    handler_t reciver =  std::bind(&HandlerConnection::HandlerReciver, &handler_connection, std::placeholders::_1); 
    handler_t sender = std::bind(&HandlerConnection::HandlerSender, &handler_connection, std::placeholders::_1);
    handler_t excepter = std::bind(&HandlerConnection::HandlerExcepter, &handler_connection, std::placeholders::_1);

    Listener listener(port);
    
    handler_t listen_connect = std::bind(&Listener::Accepter, &listener, std::placeholders::_1);
    
    R->SetListenConnection(listen_connect);
    R->SetNormalConnection(reciver, sender, excepter);
    

    InetAdrr addr("0.0.0.0", port);
    std::cout << listener.Sockfd() << std::endl;
    R->AddConnection(listener.Sockfd(), EPOLLIN, addr, ListenConnection);



    R->Dispatcher();
    return 0;
}

